package org.library.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.library.domain.LoginUser;
import org.library.domain.SysUser;
import org.library.mapper.UserMapper;
import org.springframework.stereotype.Component;

/**
 * 认证控制器
 *
 * @author Chen
 * @since 2023/6/13 14:36
 */

@Component
@RequiredArgsConstructor
public class TokenController {

    private final UserMapper userMapper;

    public LoginUser login(String phone, String password) {
        SysUser one = new LambdaQueryChainWrapper<>(userMapper).eq(SysUser::getPhone, phone)
                .one();
        if (one == null) {
            return null;
        } else if (one.getPassword().equals(password)) {
            LoginUser loginUser = new LoginUser();
            loginUser.setPhone(one.getPhone());
            loginUser.setNickname(one.getNickname());
            return loginUser;
        }
        return null;
    }

    public Boolean register(SysUser user) {
        return userMapper.insert(user) > 0;
    }
}
